﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;
using YiSha.Model.Result;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2025-03-01 16:45
    /// 描 述：业务类
    /// </summary>
    public class AttributesBLL
    {
        private AttributesService attributesService = new AttributesService();

        #region 获取数据
        public async Task<TData<List<AttributesEntity>>> GetList(AttributesListParam param)
        {
            TData<List<AttributesEntity>> obj = new TData<List<AttributesEntity>>();
            obj.Data = await attributesService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<AttributesEntity>>> GetPageList(AttributesListParam param, Pagination pagination)
        {
            TData<List<AttributesEntity>> obj = new TData<List<AttributesEntity>>();
            obj.Data = await attributesService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<ZtreeInfo>>> GetAttributesTreeListJson(AttributesListParam param)
        {
            var obj = new TData<List<ZtreeInfo>>();
            obj.Data = new List<ZtreeInfo>();
            List<AttributesEntity> departmentList = await attributesService.GetList(param);
           
            foreach (AttributesEntity attributes in departmentList)
            {
                obj.Data.Add(new ZtreeInfo
                {
                    id = attributes.Id,
                    pId = attributes.parent_attribute_id,
                    name = attributes.attribute_name
                });
            }
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<AttributesEntity>> GetEntity(long id)
        {
            TData<AttributesEntity> obj = new TData<AttributesEntity>();
            obj.Data = await attributesService.GetEntity(id);
            if (obj.Data != null)
            {
                var valueslist = attributesService.BaseRepository().FindList<AttributeValuesEntity>(t => t.attribute_id == obj.Data.Id).Result.Select(t => t.value);

                if (valueslist.Count()>0)
                {
                    obj.Data.values = string.Join(",", valueslist);
                }
                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(AttributesEntity entity)
        {
            TData<string> obj = new TData<string>();
            await attributesService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await attributesService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
